﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using roster.Domain;

namespace roster.UI
{
    public partial class RaidView : Form, IRaidView
    {
        public RaidView()
        {
            InitializeComponent();
            DialogResult = DialogResult.Cancel;
        }

        #region IRaidView Members

        public event MethodInvoker UpdateRaid;
        public event MethodInvoker CancelRaid;
        public event Action<ILootToken> TokenSelected;
        public event MethodInvoker AddPlayer;
        public event Action<IPlayer> Suicide;
        public event Action<IPlayer> MoveUp;
        public event Action<IPlayer> MoveDown;
        public event Action<IEnumerable<IPlayer>> RemovePlayer;

        public void SetTitle(string title)
        {
            Text = title;
        }

        public void SetTokens(IEnumerable<ILootToken> tokens)
        {
            cboToken.Items.AddRange(tokens.ToArray());
            cboToken.SelectedIndex = 0;
        }

        public void SelectPlayers(IEnumerable<IPlayer> players)
        {
            lstPlayers.SelectedPlayers = players;
        }


        public void UpdateView(IEnumerable<IPlayer> players)
        {
            lstPlayers.AddPlayers(players);
            Refresh();
        }

        public void SelectPlayer(IPlayer player)
        {
            lstPlayers.SelectedPlayer = player;
        }

        #endregion

        private void btnMoveUp_Click(object sender, EventArgs e)
        {
            ExecuteOnSelectedPlayer(MoveUp);
        }

        private void btnMoveDown_Click(object sender, EventArgs e)
        {
            ExecuteOnSelectedPlayer(MoveDown);
        }

        private void btnSuicide_Click(object sender, EventArgs e)
        {
            ExecuteOnSelectedPlayer(Suicide);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            UpdateRaid();
            Hide();
        }

        private void ExecuteOnSelectedPlayer(Action<IPlayer> call)
        {
            var player = lstPlayers.SelectedPlayer;
            if (player != null)
            {
                call(player);
            }
        }

        private void RaidView_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            CancelRaid();
            Hide();
        }

        private void btnAddPlayer_Click(object sender, EventArgs e)
        {
            AddPlayer();
        }

        private void cboToken_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void btnSelectToken_Click(object sender, EventArgs e)
        {
            TokenSelected((ILootToken) cboToken.SelectedItem);
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            RemovePlayer(lstPlayers.SelectedPlayers);
        }
    }
}